package com.hl.sfzy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Charge;
import com.hl.sfzy.service.ChargeService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/charge")
@CrossOrigin
public class ChargeController {
    @Autowired
    private ChargeService chargeService;
    @RequestMapping("/selectAll")
    public R selectAll(){
        List<Charge> list = chargeService.selectAll();
        return R.ok(list);
    }
    @PostMapping("/insert")
    public R insert(@RequestBody Charge charge){
        return R.ok(chargeService.insert(charge));
    }
    @DeleteMapping("/delete")
    public R delete(long id){
        return R.ok(chargeService.deleteById(id));
    }
    @PutMapping("/update")
    public R update(@RequestBody Charge charge){
        return R.ok(chargeService.update(charge));
    }
    @GetMapping("/selectById")
    public R selectById(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "3") int pageSize,
            long id){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Charge> info = new PageInfo<>(chargeService.selectByPrimaryKey(id));
        return R.ok(info);
    }
    @GetMapping("/findByPage")
    public R findByPage(@RequestParam(required = false,defaultValue = "1") int pageNum,
                        @RequestParam(required = false,defaultValue = "3") int pageSize,
                        Charge charge){
        PageInfo<Charge> pageInfo=chargeService.findByPage(pageNum, pageSize, charge);
        return R.ok(pageInfo);
    }
}
